/**
 * @Date 2023-02-28 14:13:22
 * @Remark
 */

// lib
// config & script & public
import { message, notification } from "./Interaction";
// http
// business
// 其它

function msg(text: string) {
  message.info(text);
}

function success(text: string) {
  message.success(text);
}

function warning(text: string) {
  message.warning(text);
}

function error(text: string) {
  message.error(text);
}

function successAlert(type: "add" | "update" | "del" | "audit" | "set") {
  switch (type) {
    case "add":
      toast.success("添加成功");
      break;
    case "update":
      toast.success("编辑成功");
      break;
    case "del":
      toast.success("删除成功");
      break;
    case "audit":
      toast.success("审核完成");
      break;
    case "set":
      toast.success("设置完成");
      break;
    default:
      break;
  }
}

function whenFailOrErr<T>(result: BagData<T>) {
  if (result.fail.length > 0) {
    message.warning(result.fail[0].message);
    return false;
  }
  if (result.err.length > 0) {
    notification.error({
      message: "系统异常",
      description: result.err[0].message,
    });
    return false;
  }
  return true;
}

function whenErr<T>(result: BagData<T>) {
  if (result.err.length > 0) {
    message.error(result.err[0].message);
    return false;
  }
  return true;
}

const toast = {
  msg,
  success,
  successAlert,
  warning,
  error,
  whenErr,
  whenFailOrErr,
};

export default toast;
